package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Product;
import tech.aistar.entity.ProductType;
import tech.aistar.service.IProductService;
import tech.aistar.service.IProductTypeService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/1/11 2:15 下午
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    //service层调用Mapper
    //controller调用service
    @Autowired
    private IProductTypeService productTypeService;

    @Autowired
    private IProductService productService;

    /**
     * request->一次请求一次响应.
     *
     * 返回到首页的jsp
     * @return
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request,String name,Integer typeId){
        //获取后台所有的产品的数据
        List<Product> productList = productService.findAll(name,typeId);

        //放入到request作用域中
        request.setAttribute("productList",productList);

        //获取所有的产品类型的数据
        List<ProductType> typeList = productTypeService.findAll();

        request.setAttribute("typeList",typeList);

        //为了进行产品类型的数据回显的功能 - 需要将typeId继续传回到页面
        request.setAttribute("typeId",typeId);
        request.setAttribute("name",name);


        return "product/index";
    }

    @GetMapping("/del")
    public String del(Integer id){
        productService.delById(id);
        return "redirect:/product/index";
    }
}
